<?php
namespace app\member\controller\api;
use think\Db;
use think\facade\Cache;
use app\ApiController;
use app\member\model\UsersModel;
use app\member\model\AccountLogModel;
use app\member\model\WithdrawAccountModel;
/*------------------------------------------------------ */
//-- 提现相关API
/*------------------------------------------------------ */

class Center extends ApiController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
		$this->checkLogin();//验证登陆
        $this->Model = new WithdrawAccountModel();
    }
    /*------------------------------------------------------ */
    //-- 我的签到
    /*------------------------------------------------------ */
    /*------------------------------------------------------ */
    public function signIndex()
    {
    	$user = $this->userInfo;

    	$UsersModel = new UsersModel;
        $return['code'] = 1;
    	$return['timeData'] = time();
    	$return['use_integral'] = $UsersModel->signIntegral();
        $return['is_sign']  = $UsersModel->is_sign($user['user_id']);
        $return['signTime'] = $UsersModel->signTime($user['user_id'],1);
        $return['signData'] = $UsersModel->signIndex($user['user_id'],1);
		return $this->ajaxReturn($return);
    }
    //-- 签到记录
    /*------------------------------------------------------ */
    public function signInfo()
    {
        $user = $this->userInfo;

        $page  = input('page');
        $timeY = input('timeY');
        $timeM = input('timeM');
        $month = $timeY."-".$timeM;//查询年月
	    $date[0] = strtotime($month);//指定月份月初时间戳
	    $date[1] = mktime(23, 59, 59, date('m', strtotime($month))+1, 00);//指定月份月末时间戳

        $UsersModel = new UsersModel;
        $res['info'] = $UsersModel->signInfos($user['user_id'], $date, $page, 10);

        $data[0] = date('Y年m月',time());
        $data[1] = strtotime(date('Y-m-d', time()) . '00:00:00');
        $data[2] = strtotime(date('Y-m-d', time()) . '23:59:59');
        $res['data'] = $data;
        $res['code'] = 1;
        return $this->ajaxReturn($res);
    }
	/*------------------------------------------------------ */
    //-- 签到
    /*------------------------------------------------------ */
    public function signIng()
    {
    	$user = $this->userInfo;
    	$UsersModel = new UsersModel;
        $data = $UsersModel->signIng($user['user_id']);
        if ($data == true) {
        	$res['code'] = 1;
        	$res['msg'] = '签到成功';
        	return $this->ajaxReturn($res);
        }
	}
    /*------------------------------------------------------ */
    //-- 查询区域代理
    /*------------------------------------------------------ */
    public function regionquery(){
        $cityname  = input('cityname');
        //查询省
        $res = [];
        if(empty($cityname)){
            $res = Db::table('user_region')->where('status',1)->field('user_id,level,name,phone,update_time,region')->select();
            foreach($res as $k=>$vv){
                $res[$k]['update_time'] = date('Y-m-d',$vv['update_time']);
                $res[$k]['avatar'] = Db::table('users')->where('user_id',$vv['user_id'])->value('headimgurl');
                switch ($vv['level']) {
                    case 1:
                        $res[$k]['region_name'] = Db::table('bs_province')->where('PROVINCE_CODE',$vv['region'])->value('PROVINCE_NAME');
                        break;
                    case 2:
                        $res[$k]['region_name'] = Db::table('bs_city')->where('CITY_CODE',$vv['region'])->value('CITY_NAME');
                        
                        break;
                    case 3:
                        $res[$k]['region_name'] = Db::table('bs_area')->where('AREA_CODE',$vv['region'])->value('AREA_NAME');
                        break;
                    case 4:
                        $res[$k]['region_name'] = Db::table('bs_street')->where('STREET_CODE',$vv['region'])->value('STREET_NAME');
                        break;
                    default:
                        $res = [];
                        break;
                }
                unset($res[$k]['region']);
                
            }
        }else{
            $map[]  = ['PROVINCE_NAME','like','%'.$cityname.'%'];
            $province = Db::table('bs_province')->where($map)->select();
            if($province){
                foreach($province as $value){
                    $resd = Db::table('user_region')->field('user_id,level,name,phone,update_time')->where('region',$value['PROVINCE_CODE'])->where('status',1)->find();
                    if($resd){
                        $resd['region_name'] = $value['PROVINCE_NAME'];
                        $resd['update_time'] = date('Y-m-d',$resd['update_time']);
                        $resd['avatar'] = Db::table('users')->where('user_id',$resd['user_id'])->value('headimgurl');
                        $res[] = $resd;
                    }
                }
            }
            //查询市
            $CITY_NAME[]  = ['CITY_NAME','like','%'.$cityname.'%'];
            $city = Db::table('bs_city')->where($CITY_NAME)->select();
            if($city){
                foreach($city as $cityv){
                    $resd = Db::table('user_region')->field('user_id,level,name,phone,update_time')->where('region',$cityv['CITY_CODE'])->where('status',1)->find();
                    if($resd){
                        $resd['region_name'] = $cityv['CITY_NAME'];
                        $resd['update_time'] = date('Y-m-d',$resd['update_time']);
                        $resd['avatar'] = Db::table('users')->where('user_id',$resd['user_id'])->value('headimgurl');
                        $res[] = $resd;
                    }
                }
            }
            //查询区
            $AREA_NAME[]  = ['AREA_NAME','like','%'.$cityname.'%'];
            $area = Db::table('bs_area')->where($AREA_NAME)->select();
            if($area){
                foreach($area as $areav){
                    $resd = Db::table('user_region')->field('user_id,level,name,phone,update_time')->where('region',$areav['AREA_CODE'])->where('status',1)->find();
                    if($resd){
                        $resd['region_name'] = $areav['AREA_NAME'];
                        $resd['update_time'] = date('Y-m-d',$resd['update_time']);
                        $resd['avatar'] = Db::table('users')->where('user_id',$resd['user_id'])->value('headimgurl');
                        $res[] = $resd;
                    }
                }
            }
            //查询镇
            $STREET_NAME[]  = ['STREET_NAME','like','%'.$cityname.'%'];
            $street = Db::table('bs_street')->where($STREET_NAME)->select();
            if($street){
                foreach($street as $streetv){
                    $resd = Db::table('user_region')->field('user_id,level,name,phone,update_time')->where('region',$streetv['STREET_CODE'])->where('status',1)->find();
                    if($resd){
                        $resd['region_name'] = $streetv['STREET_NAME'];
                        $resd['update_time'] = date('Y-m-d',$resd['update_time']);
                        $resd['avatar'] = Db::table('users')->where('user_id',$resd['user_id'])->value('headimgurl');
                        $res[] = $resd;
                    }
                }
            }
        }
        

        $res1['code'] = 1;
        $res1['data'] = $res;
        $res1['msg'] = '查询成功';
        return $this->ajaxReturn($res1);
    }
    /*------------------------------------------------------ */
    //-- 查询区域津贴
    /*------------------------------------------------------ */
    public function regionaward(){
        $log = Db::table('distribution_dividend_log')
        ->field('order_sn,dividend_uid,buy_uid,add_time,dividend_amount')
        ->where('award_id',4)
        ->where('dividend_uid',$this->userInfo['user_id'])
        ->where('status',1)
        ->order('log_id desc')
        ->select();
        foreach($log as $k=>$value){
            $l_user = Db::table('users')->where('user_id',$value['buy_uid'])->find();
            $log[$k]['buy_name'] = $l_user['nick_name'];
            $log[$k]['add_time'] = date('Y-m-d H:i',$value['add_time']);      
        }
        $res1['code'] = 1;
        $res1['data'] = $log;
        $res1['msg'] = '查询成功';
        return $this->ajaxReturn($res1);
    }
    
    /*------------------------------------------------------ */
    //-- 订单收入明细
    /*------------------------------------------------------ */
    public function orderaward(){
        $log = Db::table('distribution_dividend_log')->alias('log')
        ->field('log.order_sn,log.dividend_uid,log.buy_uid,log.add_time,log.level_award_name,log.dividend_amount,o.buy_goods_id as shop_id')
        ->join('shop_order_info o','o.order_id = log.order_id')
        ->where('log.dividend_uid',$this->userInfo['user_id'])
        ->where('log.status',1)
        ->order('log.log_id desc')
        ->select();
        foreach($log as $k=>$value){
            $l_user = Db::table('users')->where('user_id',$value['buy_uid'])->find();
            $log[$k]['buy_name'] = $l_user['nick_name'];
            if($l_user['headimgurl']){
                $log[$k]['avatar'] = $l_user['headimgurl'];
            }else{
                $log[$k]['avatar'] = '/static/images/avatar.png';
            }
            
            $log[$k]['add_time'] = date('Y-m-d H:i',$value['add_time']);   
            $shop = Db::table('shop_goods')->where('goods_id',$value['shop_id'])->field('goods_img,goods_name')->find();  
            $log[$k]['goods_img'] = $shop['goods_img'];
            $log[$k]['goods_name'] = $shop['goods_name'];
        }
        $res1['code'] = 1;
        $res1['data'] = $log;
        $res1['msg'] = '查询成功';
        return $this->ajaxReturn($res1);
    }
    public function awardlog(){
        $log = Db::table('distribution_dividend_log')
        ->where('dividend_uid',$this->userInfo['user_id'])
        ->where('status',1)
        ->order('log_id desc')
        ->select();
        foreach ($log as $k=>$v){
            $log[$k]['add_time'] = date('Y-m-d H:i',$v['add_time']);
        }
        $res1['code'] = 1;
        $res1['data'] = $log;
        $res1['msg'] = '查询成功';
        return $this->ajaxReturn($res1);
    }
}
